<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script>
        //案例1
        var num = 10;
        fun();

        function fun() {
            console.log(num);
            var num = 20;
        }
        //相当于执行一下操作
        var num;

        function fun() {
            var num; //变量声明提前 然后按照顺序解析
            console.log(num);
            num = 20;
        }
        num = 10;
        fun();


        //案例2
        var num = 10;

        function fn() {
            console.log(num);
            var num = 20;
            console.log(num);
        }
        fn();
        //相当于执行一下操作
        var num;

        function fn() {
            var num;
            console.log(num);
            num = 20;
            console.log(num);
        }
        num = 10;
        fn();


        //案例3
        var a = 18;
        f1();

        function fn() {
            var b = 9;
            console.log(a);
            console.log(b);
            var a = '123';
        }
        //相当于执行一下操作
        var a;

        function fn() {
            var a;
            var b;
            b = 9;
            console.log(a); // var a 里这句话最近（按照执行过程） 所以这里是没被赋值的 得到的就是好undefined
            console.log(b);
            a = '123';
        }
        a = 18;
        f1();


        //案例4
        f1();
        console.log(c);
        console.log(b);
        console.log(a);

        function f1() {
            var a = b = c = 9; //这句话等于 var a = 9  b = 9  c = 9
            //如果是要都声明的话 是需要用逗号隔开的  
            //比如 var a = 9,b = 9,c = 9  用逗号隔开了！！
            console.log(a);
            console.log(b);
            console.log(c);
        }
        //相当于执行一下操作
        function f1() {
            var a;
            a = b = c = 9; //这个是全局变量 所以外边的b c是等于9  为什么是全局变量？  因为函数内部为它们被直接赋值 没有var声明 当全局变量看
            console.log(a); //9
            console.log(b); //9
            console.log(c); //9
        }
        f1();
        console.log(c); //9
        console.log(b); //9
        console.log(a); //undefined
    </script>
</head>

<body>

</body>

</html>